<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\enum\UserRole;
use addons\tongcheng\model\BaseModel;


class DeliveryOrderActionModel extends BaseModel
{


    // 表名
    protected $name = 'tongcheng_delivery_order_action';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'operate_role_text',
        'createtime_text'
    ];


    public function getOperateRoleList()
    {
        return UserRole::getValueDescription();
        return ['CUSTOMER' => __('Customer'), 'RIDER' => __('Rider'), 'ADMIN' => __('Admin'), 'SYSTEM' => __('System')];
    }


    public function getOperateRoleTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['operate_role']) ? $data['operate_role'] : '');
        $list  = $this->getOperateRoleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function order()
    {
        return $this->belongsTo(DeliveryOrderModel::class, 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 订单用户操作记录
     * @param $user_id
     * @param $order_id
     * @param $order_status
     * @param $remark
     * @param $refund_status
     * @return void
     */
    public static function createFromCustomer($user_id, $order_id, $order_status, $remark = '', $refund_status = 0)
    {
        self::create([
            'order_id'        => $order_id,
            'order_status'    => $order_status,
            'refund_status'   => $refund_status,
            'operate_role'    => UserRole::CUSTOMER,
            'operate_user_id' => $user_id,
            'remark'          => $remark,
        ]);
    }

    /**
     * 订单骑手操作记录
     * @param $user_id
     * @param $order_id
     * @param $order_status
     * @param $remark
     * @param $refund_status
     * @return void
     */
    public static function createFromRider($user_id, $order_id, $order_status, $remark = '', $refund_status = 0)
    {
        self::create([
            'order_id'        => $order_id,
            'order_status'    => $order_status,
            'refund_status'   => $refund_status,
            'operate_role'    => UserRole::RIDER,
            'operate_user_id' => $user_id,
            'remark'          => $remark,
        ]);
    }

    /**
     * 订单管理员操作记录
     * @param $user_id
     * @param $order_id
     * @param $order_status
     * @param $remark
     * @param $refund_status
     * @return void
     */
    public static function createFromAdmin($user_id, $order_id, $order_status, $remark = '', $refund_status = 0)
    {
        self::create([
            'order_id'        => $order_id,
            'order_status'    => $order_status,
            'refund_status'   => $refund_status,
            'operate_role'    => UserRole::ADMIN,
            'operate_user_id' => $user_id,
            'remark'          => $remark,
        ]);
    }

    /**
     * 订单系统操作记录
     * @param $user_id
     * @param $order_id
     * @param $order_status
     * @param $remark
     * @param $refund_status
     * @return void
     */
    public static function createFromSystem($user_id, $order_id, $order_status, $remark = '', $refund_status = 0)
    {
        self::create([
            'order_id'        => $order_id,
            'order_status'    => $order_status,
            'refund_status'   => $refund_status,
            'operate_role'    => UserRole::SYSTEM,
            'operate_user_id' => $user_id,
            'remark'          => $remark,
        ]);
    }

    /**
     * 根据操作人角色创建订单操作记录
     * @param $role
     * @param $user_id
     * @param $order_id
     * @param $order_status
     * @param $remark
     * @param $refund_status
     * @return void
     */
    public static function createWithRole($role, $user_id, $order_id, $order_status, $remark = '', $refund_status = 0)
    {
        switch ($role) {
            case UserRole::CUSTOMER:
                self::createFromCustomer($user_id, $order_id, $order_status, $remark, $refund_status);
                break;

            case UserRole::RIDER:
                self::createFromRider($user_id, $order_id, $order_status, $remark, $refund_status);
                break;

            case UserRole::ADMIN:
                self::createFromAdmin($user_id, $order_id, $order_status, $remark, $refund_status);
                break;

            case UserRole::SYSTEM:
                self::createFromSystem($user_id, $order_id, $order_status, $remark, $refund_status);
                break;

            default:
                break;
        }
    }
}
